﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceFight
{
    public class Camera : Microsoft.Xna.Framework.GameComponent
    {
        public Matrix view { get; set; }
        public Matrix projection { get; set; }
        protected GraphicsDevice graphicsDevice { get; set; }

        public float rotationX { get; set; }
        public float rotationY { get; set; }

        public float minRotationY { get; set; }
        public float maxRotationY { get; set; }

        public float distance { get; set; }

        public float minDistance { get; set; }
        public float maxDistance { get; set; }

        public Vector3 position { get; protected set; }
        public Vector3 target { get; protected set; }

        MouseState prevMouseState;

        public Camera(Game game, GraphicsDevice graphicsDevice, Vector3 target, float rotationX, float rotationY, float minRotationY, float maxRotationY, float distance, 
            float minDistance, float maxDistance)
            : base(game)
        {
            this.graphicsDevice = graphicsDevice;
            this.target = target;


            this.rotationY = MathHelper.Clamp(rotationY, minRotationY, maxRotationY);
            this.rotationX = rotationX;

            this.minDistance = minDistance;
            this.maxDistance = maxDistance;

            this.distance = MathHelper.Clamp(distance, minDistance, maxDistance);

            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height, 0.1f, 100000.0f);
        }

        public override void Initialize()
        {
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
            prevMouseState = Mouse.GetState();

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            
            this.rotationX += ((float)prevMouseState.X - (float)Mouse.GetState().X) *.01f;
            this.rotationY += ((float)prevMouseState.Y - (float)Mouse.GetState().Y) * .01f;
            this.rotationY = MathHelper.Clamp(rotationY, minRotationY, maxRotationY);

            if (prevMouseState.ScrollWheelValue < Mouse.GetState().ScrollWheelValue && this.distance != 10)
            {
                this.distance -= 10;

            }
            else if (prevMouseState.ScrollWheelValue > Mouse.GetState().ScrollWheelValue && this.distance != 100)
            {
                this.distance += 10;
            }

            Matrix rotation = Matrix.CreateFromYawPitchRoll(rotationX, -rotationY, 0);

            Vector3 translation = new Vector3(0, 0, distance);
            translation = Vector3.Transform(translation, rotation); 

            Vector3 up = Vector3.Transform(Vector3.Up, rotation);

            position = target + translation + new Vector3(0, 10f, 0.6f);

            view = Matrix.CreateLookAt(position, target, up);

            prevMouseState = Mouse.GetState();
            //Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
            
            base.Update(gameTime);
        }

        public void SetTarget(Vector3 target)
        {
            this.target = target;
        }

        public void Translate(Vector3 positionChange)
        {
            this.position += positionChange;
        }




        
    }
}
